<template>
  <div class="cont">
    <!-- 搜索工作栏 -->
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="cont_top" label-width="50px">
      <el-form-item label="业务类型" prop="type" style="margin-right:17px;" label-width="74px">
        <el-select v-model="queryParams.type" @change="handleQuery" placeholder="" placement="bottom"
          style="width: 200px;">
          <el-option v-for="item in optiontype" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name" style="margin-right:17px;">
        <el-input v-model="queryParams.name" placeholder="" @keyup.enter="handleQuery" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="预约时间" prop="appointTime" label-width="74px">
        <el-date-picker v-model="queryParams.appointTime"
          :default-createTime="[new Date('1 00:00:00'), new Date('1 23:59:59')]" style="width: 200px;"
          end-placeholder="结束日期" start-placeholder="开始日期" type="daterange" @change="handleQuery" value-format="x" />
      </el-form-item>
      <el-form-item style="margin-right:0;">
        <el-button @click="handleQuery" class="btn btnsearch">查询</el-button>
        <el-button @click="resetQuery" class="btn btnreset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table v-loading="loading" element-loading-text="Loading..." :data="list"
      style="margin-bottom: 20px;min-height: 435px;" :header-cell-style="{ background: '#F0F0F0' }">
      <el-table-column align="center" show-overflow-tooltip label="业务类型" prop="type" width="300">
        <template #default="{ row }">
          <div class="namecont">
            【{{ typeTypeObj[row.type] }}】{{ typeDetailTypeObj[row.detailType] }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip label="补充说明" prop="remark" min-width="300">
        <template #default="scope">
          <div class="namecont">
            {{ scope.row.remark === "" ? '--' : scope.row.remark }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip label="姓名" prop="name" min-width="160">
        <template #default="scope">
          <div class="namecont">
            {{ scope.row.name }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系方式" prop="contactMethod" min-width="200" />
      <el-table-column align="center" label="预约时间" prop="appointTime" min-width="200">
        <template #default="scope">
          <div class="time_style">{{ formatTime(scope.row.appointTime, 'yyyy-MM-dd') }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right" width="120">
        <template #default="scope">
          <el-button link type="primary" class="btnhover" @click="delbtn(scope.row)"
            style="color: #3C88F0;">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination style="font-size: 14px;" v-model:current-page="queryParams.pageNo" :hide-on-single-page="value"
      v-model:page-size="queryParams.pageSize" :page-sizes="pageSizes" layout="total,sizes,prev, pager, next,jumper"
      :total="total" @size-change="selectchange" @current-change="handleCurrentChange" />
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { formatTime } from '@/utils'
import * as getGovernmentApi from '@/api/governmentAppointment/index'
const loading = ref(true) // 列表加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const value = ref(true)
const pageSizes = [10, 20, 50, 100]
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type: '',
  name: '',
  appointTime: []
})
const queryFormRef = ref() // 搜索的表单
const typeDetailTypeObj = {}
const typeTypeObj = {}
/** 条数变化 */
const selectchange = (e) => {
  queryParams.pageSize = e
  queryParams.pageNo = 1
  getList()
}
/** 页码变化 */
const handleCurrentChange = (e) => {
  queryParams.pageNo = e
  getList()
}
/** 获取预约数据 */
const optiontype = ref([])
const getDictData = () => {
  const params = {
    pageNo: 1,
    pageSize: 100,
    dictType: 'countryside_appointment_detail_type'
  }
  getGovernmentApi.getDictData(params).then((res) => {
    const items = res.list.reverse().filter((item, index, self) => {
      return self.findIndex((t) => t.value === item.value) === index
    })
    optiontype.value = categorizeData(items)
    items.forEach(item => {  
      typeDetailTypeObj[item.value] = item.label.replace(/【.*?】/, '')
    });  
    optiontype.value.forEach(item => {  
      typeTypeObj[item.value] = item.label
    });  
  })
}
// 处理大分类
function categorizeData(data) {
  const categories = new Map()
  data.forEach((item) => {
    const match = item.label.match(/【([^】]+)】/)
    if (match && match[1]) {
      const category = match[1]
      if (!categories.has(category)) {
        categories.set(category, String(categories.size + 1));
      }
    }
  })
  return Array.from(categories.entries()).map(([label, value]) => ({ label, value }))
}

/** 查询任务列表 */
const getList = async () => {
  getGovernmentApi.getGovernmentPage(queryParams).then((res) => {
    let data = reactive(res.list)
    list.value = data;
    total.value = res.total
    if (list.value.length === 0) {
      value.value = true
    } else {
      value.value = false
    }
    loading.value = false
  }).catch(error => {
    loading.value = false
  })
}

/** 删除 */
const delbtn = (row: any) => {
  ElMessageBox.confirm(
    '此操作将永久删除该评价, 是否继续?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      getGovernmentApi.deleteGovernmentList([row.id]).then((res) => {
        if (res === true) {
          ElMessage.success('删除成功！')
          getList()
        }
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

/** 查询按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getDictData()
  getList()
})
</script>


<style scoped>
.cont {
  background-color: rgba(255, 255, 255, 1);
  color: rgba(16, 16, 16, 1);
  font-size: 14px;
  text-align: center;
  min-height: 557px;
  position: relative;
}

.cont_top {
  text-align: left;
  margin: 4px 0;
}

::v-deep .el-table .cell {
  display: flex;
  justify-content: center;
}

::v-deep .el-input__wrapper.is-focus {
  box-shadow: 0 0 0 1px #CC352F;
}

::v-deep .el-range-editor.is-active {
  box-shadow: 0 0 0 1px #CC352F;
}

::v-deep .el-select__wrapper.is-focused {
  box-shadow: 0 0 0 1px #CC352F;
}

.btn {
  width: 80px;
  height: 30px;
  line-height: 20px;
  border-radius: 5px;
  font-size: 14px;
  text-align: center;
}

.btn:hover {
  opacity: 0.8;
}

.btnreset {
  background-color: rgba(255, 255, 255, 1);
  border: 1px solid rgba(187, 187, 187, 1);
  color: rgba(142, 142, 142, 1);
}

.btnsearch {
  background-color: #CC352F;
  color: rgba(255, 255, 255, 1);
  border: 1px solid #CC352F;
}

.btnhover:hover {
  opacity: 0.8;
}

.el-pagination {
  justify-content: center;
}

.time_style {
  font-size: 14px;
  color: #CC352F;
}

::v-deep .el-form-item__label {
  padding: 0 17px 0 0;
}

.namecont {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>